﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VehicleWeaponsMod
{
    public class VehicleWeaponsShop
    {
        public GTA.Camera ShopCam;
        public uint ShopCheckpoint;
        public GTA.Blip ShopBlip;
        public GTA.Vector3 ShopBlipLoc;
        public GTA.Vector3 ShopBlipDirection;
        public GTA.Vector3 ShopExitLoc;
        public GTA.Vector3 ShopExitDirection;
        public GTA.Vector3 GarageVehicleLoc;
        public GTA.Vector3 GarageVehicleDirection;
        public GTA.Vector3 GarageCamLoc;
        public GTA.Vector3 GarageCamDirection;
        public VehicleWeaponsForm ShopForm;
        private uint timeinblip = 0;
        public bool IsShopping = false;
        public bool IsBlipVisible = false;

        public VehicleWeaponsShop()
        {
            ShopBlip = new GTA.Blip();
            GTA.Events.RunTick.Handler += Tick;
        }

        unsafe public void EnterShop()
        {
            // teleport to shop
            if (IsShopping) return;
            GTA.Ped plped = (new GTA.Player().GetPlayerPed());
            GTA.Vehicle plveh = plped.GetVehiclePedIsDriving();
            if (!GTA.Vehicle.Exists(plveh))
            {
                GTA.Screen.PrintString("You need a vehicle to enter the Vehicle Weapons shop!",1000);
                return;
            }
            IsShopping = true;
            GTA.ScriptApi.PlayAudioEvent("FRONTEND_GAME_PICKUP_CHECKPOINT");

            GTA.ScriptApi.DisplayCash(true);
            GTA.ScriptApi.SetPlayerControl(new GTA.Player(), false);

            GTA.ScriptApi.SetCarCoordinates(plveh, GarageVehicleLoc.X, GarageVehicleLoc.Y, GarageVehicleLoc.Z);
            GTA.ScriptApi.SetCarHeading(plveh, GarageVehicleDirection.Y);
            ShopCam = new GTA.Camera();
            //uint unk_CamCommands=0;
            //GTA.ScriptApi.BeginCamCommands(&unk_CamCommands);
            GTA.ScriptApi.CreateCam(14, ShopCam);
            GTA.ScriptApi.SetCamPos(ShopCam, GarageCamLoc.X, GarageCamLoc.Y, GarageCamLoc.Z);
            GTA.ScriptApi.SetCamRot(ShopCam, GarageCamDirection.X, GarageCamDirection.Y, GarageCamDirection.Z);
            GTA.ScriptApi.SetCamActive(ShopCam, true);
            GTA.ScriptApi.SetCamPropagate(ShopCam, true);
            GTA.ScriptApi.ActivateScriptedCams(1, 1);

            ShopForm = new VehicleWeaponsForm();
            ShopForm.Shop = this;
            ShopForm.UseMouse = true;
            ShopForm.Visible = true;
            GTA.Screen.AddFormA(ShopForm); // add the form to screen
            Log.Debug("Enterd shop");
        }

        unsafe public void ExitShop()
        {
            // teleport back to street
            ShopForm.UseMouse = false;
            ShopForm.Visible = false;
            GTA.Screen.RemoveForm(ShopForm);
            //ShopForm.Dispose();
            ShopForm = null;

            GTA.Ped plped = (new GTA.Player().GetPlayerPed());
            GTA.Vehicle plveh = plped.GetVehiclePedIsDriving();
            if (GTA.Vehicle.Exists(plveh))
            {
                GTA.ScriptApi.SetCarCoordinates(plveh, ShopExitLoc.X, ShopExitLoc.Y, ShopExitLoc.Z);
                GTA.ScriptApi.SetCarHeading(plveh, ShopExitDirection.Y);
            }

            if (ShopCam != null)
            {
                GTA.ScriptApi.SetCamActive(ShopCam, false);
                GTA.ScriptApi.SetCamPropagate(ShopCam, false);
                GTA.ScriptApi.DestroyCam(ShopCam);
                GTA.ScriptApi.ActivateScriptedCams(0,0);
                //uint unk_CamCommands=0;
                //GTA.ScriptApi.EndCamCommands(&unk_CamCommands);
                ShopCam = null;
            }

            GTA.ScriptApi.SetPlayerControl(new GTA.Player(), true);
            GTA.ScriptApi.DisplayCash(false);
            GTA.ScriptApi.PlayAudioEvent("FRONTEND_GAME_PICKUP_CHECKPOINT");
            IsShopping = false;
            Log.Debug("Exited shop");
        }

        unsafe public void Tick(Object sender, EventArgs args)
        {
            if ((ShopBlipLoc == null) || (ShopBlipDirection == null) || 
                (GarageVehicleLoc == null) || (GarageVehicleDirection == null) || 
                (GarageCamLoc == null) || (GarageCamDirection == null) ||
                (ShopExitLoc == null) || (ShopExitDirection == null))
                return; // NO SHOP BLIP

            if (IsShopping) GTA.ScriptApi.DisplayCash(true);

            GTA.Ped plped = (new GTA.Player().GetPlayerPed());
            GTA.Vector3 plpos = plped.Coordinates;

            if (GTA.World.GetDistance(plpos, ShopBlipLoc) < 3.0F)
            { // at shop door
                if (timeinblip < 20) timeinblip++;
                if (timeinblip >= 20)
                { // enter shop
                    float speed = 0.0F;
                    GTA.ScriptApi.GetCharSpeed(plped, &speed);
                    if (speed <= 1.0F)
                    {
                        Log.Debug("Enter shop by blip");
                        if (!IsShopping) 
                        {
                            timeinblip = 0; 
                            EnterShop(); 
                        }
                    }
                    else
                    {
                        //Log.Debug("moving too fast to enter blip");
                    }
                }
            }
            else
            { // not at shop door
                timeinblip = 0;
            }


            if (GTA.World.GetDistance(plpos, ShopBlipLoc) < 100.0F)
            {
                if (!IsBlipVisible)
                {
                    Log.Debug("Adding shop blip");
                    GTA.ScriptApi.AddBlipForCoord(ShopBlipLoc.X, ShopBlipLoc.Y, ShopBlipLoc.Z, ShopBlip);
                    //GTA.ScriptApi.ChangeBlipDisplay(ShopBlip, (GTA.eBlipDisplay)2);
                    GTA.ScriptApi.ChangeBlipSprite(ShopBlip, GTA.eBlipSprite.BLIP_WEAPONS);
                    int colint = 1;
                    GTA.ScriptApi.ChangeBlipColour(ShopBlip, (uint)colint);
                    //GTA.ScriptApi.FlashBlipAlt(ShopBlip, true);
                    //GTA.ScriptApi.ChangeBlipPriority(ShopBlip,(GTA.eBlipPriority)3);
                    ShopCheckpoint = GTA.ScriptApi.CreateCheckpoint(6, ShopBlipLoc.X, ShopBlipLoc.Y, ShopBlipLoc.Z, ShopBlipLoc.X, ShopBlipLoc.Y, ShopBlipLoc.Z-1.0F, 0.5F);

                    IsBlipVisible = true;
                    Log.Debug("shop blip added");
                }

            }
            else
            {
                if (IsBlipVisible)
                {
                    Log.Debug("removing shop blip");
                    GTA.ScriptApi.RemoveBlip(ShopBlip);
                    GTA.ScriptApi.DeleteCheckpoint(ShopCheckpoint);
                    IsBlipVisible = false;
                }
            }
        }

        public void CleanUp()
        {
            Log.Debug("VehicleWeaponShop.CleanUp()");
            if (IsShopping) ExitShop();

            if (IsBlipVisible)
            {
                GTA.ScriptApi.RemoveBlip(ShopBlip);
                GTA.ScriptApi.DeleteCheckpoint(ShopCheckpoint);
                IsBlipVisible = false;
            }
            Log.Debug("VehicleWeaponShop.CleanUp() Done");
        }

    }
}




